package cn.swu.lzr.page;

import cn.swu.lzr.JDBC.DBTools;
import cn.swu.lzr.JDBC.DBUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.io.Writer;
import java.sql.SQLException;

public class CtrlPage extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        Pager pager = new Pager();
        DBTools dbTools = new DBTools();

        try {
            pager.setTotal( dbTools.getCount());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        pager.setCurrentPage(Integer.valueOf((String) session.getAttribute("CurrentPage")));
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(pager);


        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        try (Writer writer =  response.getWriter()){
            writer.write(json);
        }
    }


}
